﻿using System.Web.UI;
using System;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace YhmOA.Common
{
    #region Page类的子类BasePage
    /// <summary>
    /// Page类的子类BasePage
    /// </summary>
    public class BasePage : Page
    {
        #region 预处理
        /// <summary>
        /// 预处理
        /// </summary>
        private Pretreatment pretreatment;
        /// <summary>
        /// 获取预处理
        /// </summary>
        internal Pretreatment Pretreatment
        {
            get
            {
                if (pretreatment == null)
                    pretreatment = new Pretreatment(this.Page, this.GetResponseType);
                return pretreatment;
            }
        }
        #endregion

        #region 重写OnLoad事件
        /// <summary>
        /// 重写OnLoad事件
        /// </summary>
        /// <param name="e">事件参数</param>
        protected override void OnLoad(EventArgs e)
        {
            //在页面上显示服务器函数的javascript的定义
            //在页面上注册一个隐藏域用来存放是否使用ajax提交
            SetPostBackEventReference();
            //this.SaveControlState();
            //向客户端注册带AjaxMethodAttribute属性的方法
            this.Pretreatment.RegisterFunctionToClient();
            //向客户端注册方法并处理响应请求
            this.Pretreatment.HandleRequest();
            base.OnLoad(e);
        }
        #endregion

        #region 重写OnPreRender事件
        /// <summary>
        /// 重写OnPreRender事件
        /// </summary>
        /// <param name="e">参数</param>
        protected override void OnPreRender(EventArgs e)
        {
            this.ServerControlAddFunction(this.Form);
            base.OnPreRender(e);
        }
        #endregion

        #region 重写Render事件
        /// <summary>
        /// 重写Render事件
        /// </summary>
        /// <param name="writer">输出流</param>
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            this.Pretreatment.Render();
        }
        #endregion

        #region 为服务端控件添加客户端方法
        /// <summary>
        /// 为服务端控件添加客户端方法
        /// </summary>
        /// <param name="control">要添加的控件</param>
        private void ServerControlAddFunction(Control control)
        {
            try
            {
                Button button = control as Button;
                string commandName = button.CommandName;
                //没有权限的时候执行
                if (commandName == "" && button.OnClientClick.IndexOf("; __doPostBack('" + button.UniqueID + "', '');return false;") < 0)
                {
                    button.OnClientClick = button.OnClientClick + "; __doPostBack('" + button.UniqueID + "', '');return false;";
                }
                else if (commandName != "" && button.OnClientClick.IndexOf("RoleVerificationCtrl") < 0)//有权限的时候执行
                {
                    button.OnClientClick = button.OnClientClick + ";RoleVerificationCtrl('" + commandName + "','" + button.UniqueID + "');";
                }
            }
            catch { }
            ControlCollection controls = control.Controls;
            foreach (Control ctrl in controls)
            {
                this.ServerControlAddFunction(ctrl);
            }
        }
        #endregion

        #region 重写EnableEventValidation属性，强制页面不验证验证回发事件
        /// <summary>
        /// <para>重写EnableEventValidation属性</para>
        /// <para>强制页面不验证验证回发事件</para>
        /// </summary>
        public override bool EnableEventValidation
        {
            get
            {
                return false;
            }
            set
            {
                base.EnableEventValidation = false;
            }
        }
        #endregion

        #region 重写VerifyRenderingInServerForm方法
        /// <summary>
        /// 重写VerifyRenderingInServerForm方法
        /// </summary>
        /// <param name="control"></param>
        public override void VerifyRenderingInServerForm(Control control)
        {
        }
        #endregion

        #region 在页面上显示服务器函数的javascript的定义
        /// <summary>
        /// <para>在页面上显示服务器函数的javascript的定义</para>
        /// <para>在页面上注册一个隐藏域用来存放是否使用ajax提交</para>
        /// </summary>
        private void SetPostBackEventReference()
        {
            this.Page.ClientScript.GetPostBackEventReference(this.Page, "");
            this.Page.ClientScript.RegisterHiddenField("__LPLAJAX", "true");
        }
        #endregion

        #region 执行客户端脚本
        /// <summary>
        /// 执行客户端脚本
        /// </summary>
        /// <param name="script">要执行的脚本</param>
        public void ExecClientScript(string script)
        {
            this.pretreatment.ExecClientScript(script);
        }
        #endregion

        #region 向客户端弹出选择窗口
        /// <summary>
        /// 向客户端弹出选择窗口
        /// </summary>
        /// <param name="text">提示框中显示的文字</param>
        /// <param name="okScript">选择是的时候执行的脚本</param>
        /// <param name="noScript">选择否的时候执行的脚本</param>
        public void Confirm(string text, string okScript, string noScript)
        {
            this.pretreatment.Confirm(text, okScript, noScript);
        }
        #endregion

        #region 向客户端弹出选择窗口
        /// <summary>
        /// 
        /// </summary>
        /// <param name="text">提示框中显示的文字</param>
        /// <param name="okScript">选择是的时候执行的脚本</param>
        public void Confirm(string text, string okScript)
        {
            this.pretreatment.Confirm(text, okScript);
        }
        #endregion

        #region 设置控件获取焦点
        /// <summary>
        /// 设置控件获取焦点
        /// </summary>
        /// <param name="clientId">要获取焦点的控件ID</param>
        public void Focus(string clientId)
        {
            this.pretreatment.Focus(clientId);
        }
        #endregion

        #region 设置控件获取焦点并将其文本选中
        /// <summary>
        /// 设置控件获取焦点并将其文本选中
        /// </summary>
        /// <param name="clientId">要选中文本的控件ID</param>
        public void Select(string clientId)
        {
            this.pretreatment.Select(clientId);
        }
        #endregion

        #region 将空间设置为ajax控件
		 /// <summary>
        /// 将空间设置为ajax控件
		 /// </summary>
		 /// <param name="control"></param>
        public void SetAjaxRenderControls(Control control)
        {
            try
            {
                ((WebControl)control).Attributes.Remove("notAjaxRender");
            }
            catch (Exception) { }
            try
            {
                (control as HtmlControl).Attributes.Remove("notAjaxRender");
            }
            catch (Exception) { }
        }
        #endregion

        #region 将空间设置为非ajax控件
        /// <summary>
        /// 将空间设置为非ajax控件
        /// </summary>
        /// <param name="control"></param>
        public void SetNotAjaxRenderControls(Control control)
        {
            try
            {
                ((WebControl)control).Attributes.Add("notAjaxRender", "notAjaxRender");
            }
            catch (Exception) { }
            try
            {
                (control as HtmlControl).Attributes.Add("notAjaxRender", "notAjaxRender");
            }
            catch (Exception) { }
        }
        #endregion

        #region 向客户端弹出一个对话框
        /// <summary>
        /// 向客户端弹出一个对话框
        /// </summary>
        /// <param name="message">对话框中显示的消息</param>
        public void MessageBox(string message)
        {
            this.pretreatment.MessageBox(message);
        }
        #endregion

        #region 向客户端弹出DIV层
        /// <summary>
        /// 向客户端弹出DIV层
        /// </summary>
        /// <param name="url">页面</param>
        /// <param name="title">页面标题</param>
        /// <param name="id">页面ID</param>
        /// <param name="width">页面的宽</param>
        /// <param name="height">页面的高</param>
        public void Dialog(string url, string title, string id, int width, int height)
        {
            this.pretreatment.Dialog(url, title, id, width, height);
        }
        #endregion

        #region 判断是否Ajax回发
        /// <summary>
        /// 判断是否Ajax回发
        /// </summary>
        public bool IsAjaxPostBack
        {
            get { return this.pretreatment.IsLplPostBack; }
        }
        #endregion

        #region 从本页面跳转到相应的页面
        /// <summary>
        /// 从本页面跳转到相应的页面
        /// </summary>
        /// <param name="url">要跳转到得页面地址</param>
        public void Redirect(string url)
        {
            this.pretreatment.Redirect(url);
        }
        #endregion

        #region 重写视图状态保存的位置
        /// <summary>
        /// 重写视图状态保存的位置
        /// </summary>
        protected override PageStatePersister PageStatePersister
        {
            get
            {
                return PageStatePersisterFactory.GetPageStatePersister(this.Page, this.GetPageStatePersisterType);
            }
        }
        #endregion

        #region 定义虚方法用来设置页面视图状态保存的位置
        /// <summary>
        /// 定义虚方法用来设置页面视图状态保存的位置
        /// </summary>
        public virtual PageStatePersisterType GetPageStatePersisterType
        {
            get
            {
                return PageStatePersisterType.Cache;
            }
        }
        #endregion

        #region 定义虚方法用来设置页面输出格式
        /// <summary>
        /// 定义虚方法用来设置页面输出格式
        /// </summary>
        public virtual ResponseType GetResponseType
        {
            get
            {
                return ResponseType.text;
            }
        }
        #endregion
    }
    #endregion
}
